package org.seedltd.upms.controller.sys;

import javax.validation.constraints.NotNull;

import org.seedltd.core.base.BaseController;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.utils.Assert;
import org.seedltd.core.utils.BeanUtils;
import org.seedltd.core.validation.Save;
import org.seedltd.core.validation.Update;
import org.seedltd.log.annotation.SysLog;
import org.seedltd.upms.model.sys.entity.SysUser;
import org.seedltd.upms.model.sys.form.SysUserForm;
import org.seedltd.upms.model.sys.form.SysUserSearchForm;
import org.seedltd.upms.model.sys.vo.SysUserVO;
import org.seedltd.upms.service.sys.SysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;

/**
 * 用户管理控制器
 *
 * @author laizuan
 * @version 1.0
 * @since 2021/07/19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sysUser")
public class SysUserController extends BaseController<SysUserService> {

    /**
     * 新增
     *
     * @param form
     *            用户管理表单数据
     * @return 用户管理主键
     */
    @SysLog("新增用户数据")
    @PostMapping(value = "/add")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public Long add(@RequestBody @Validated(Save.class) SysUserForm form) {
        SysUser sysUser = baseService.add(form);
        return sysUser.getUserId();
    }

    /**
     * 获取详情
     *
     * @param userId
     *            用户管理主键
     * @return 用户管理
     */
    @GetMapping(value = "/get")
    public SysUserVO get(Long userId) {
        Assert.notNull(userId, "参数不能为空");
        SysUser pojo = baseService.findById(userId);
        Assert.notNull(pojo, "未找到数据");
        SysUserVO pojoVo = new SysUserVO();
        BeanUtils.copyProperties(pojo, pojoVo);
        pojoVo.setDeptIdList(baseService.listUserDeptByUserId(userId));
        pojoVo.setRoleIdList(baseService.listUserRoleByUserId(userId));
        return pojoVo;
    }

    /**
     * 修改
     *
     * @param form
     *            用户管理表单数据
     * @return 修改状态。true标识修改成功，false标识修改失败
     */
    @SysLog("修改用户数据")
    @PostMapping(value = "/update")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public boolean update(@RequestBody @Validated({Update.class, Save.class}) SysUserForm form) {
        return baseService.update(form);
    }

    /**
     * 修改用户状态
     * 
     * @param status
     *            状态
     * @param userId
     *            用户主键
     * @return
     */
    @SysLog("修改用户状态")
    @PreAuthorize("hasAuthority('sys:user:update')")
    @PostMapping(value = "/update/{status}/{userId}")
    public boolean updateStatus(@PathVariable @NotNull() Integer status, @NotNull() @PathVariable Long userId) {
        return baseService.updateUserStatusById(userId, status);
    }

    /**
     * 列表
     *
     * @param searchForm
     *            查询条件
     * @return 用户管理列表集合
     */
    @PostMapping(value = "/list")
    public BasePage<SysUserVO> page(@RequestBody SysUserSearchForm searchForm) {
        return baseService.page(searchForm);
    }

}